<?php

/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 */

/**
 *
 * @author "Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>"
 *
 */
class EntityDefinition
{

  /**
   * Table name
   *
   * @var string
   */
  protected $table;

  public $join;

  /**
   * Table's fields definitions
   *
   * @var array
   */
  protected $fields = array();

  /**
   * Primary key field(s)
   *
   * @var string|array
   */
  protected $pKey;

  /**
   * Constructor.
   *
   * @param array|IDictionary $params
   * @return void
   */
  public function __construct($params = null)
  {
    if (! empty($params)) {
      $params = Dictionary::parseFrom($params);

      $this->setTable($params->get('table'));

      $this->join = $params->get('join');

      if (is_array($params->get('fields'))) {
        foreach ($params->get('fields') as $name => $params) {
          $this->addField($name, $params);
        }
      }
    }
  }

  /**
   *
   * @param mixed $def
   * @return EntityDefinition
   */
  public static function parseFrom($def)
  {
    if (! is_a($param, 'EntityDefinition')) {
      $def = new EntityDefinition($def);
    }

    return $def;
  }

  /**
   *
   * @return array
   */
  public function getDefinition()
  {
    $def = array(
      'table' => $this->getTable()
    );

    if (! empty($this->fields)) {
      $def['fields'] = array();
      foreach ($this->fields as $name => $field) {
        $def['fields'][$name] = $field->getDefinition();
      }
    }

    return $def;
  }

  public function addField($name, $def)
  {
    if (! is_a($def, 'FieldDefinition')) {
      if (! sy_get_param($def, 2) || !($class = sy_get_param($def[2], 'definition_class'))) {
        $class = 'FieldDefinition';
      }

      $def = new $class(sy_get_param($def, 0), sy_get_param($def, 1), sy_get_param($def, 2));
    }

    $this->fields[$name] = $def;

    if ($def->getPrimaryKey()) {
      if (empty($this->pKey)) {
        $this->pKey = $name;
      }

      else if (is_array($this->pKey)) {
        $this->pKey[] = $name;
      }

      else {
        $this->pKey = ( array ) $this->pKey;
        $this->pKey[$name];
      }
    }
  }

  public function getPrimaryKey()
  {
    return $this->pKey;
  }

  public function setTable($table)
  {
    $this->table = $table;
  }

  public function getTable()
  {
    return $this->table;
  }

  public function setFields($fields)
  {
    $this->fields = $fields;
  }

  public function getFields()
  {
    return $this->fields;
  }

  public function getField($name)
  {
    if (! isset($this->fields[$name])) {
      throw new EntityDefinitionException('Field not found in entity: <b>' . $name . '</b>');
    }

    return $this->fields[$name];
  }

}

?>